package com.ljw.community.service.impl;

import com.ljw.community.entity.*;
import com.ljw.community.mapper.CommentMapper;
import com.ljw.community.mapper.CommonMapper;
import com.ljw.community.mapper.TopicMapper;
import com.ljw.community.mapper.UserMapper;
import com.ljw.community.service.TopicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TopicServiceImpl implements TopicService {

    @Resource
    private TopicMapper topicMapper;
    @Resource
    private CommonMapper commonMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommentMapper commentMapper;

    @Override
    public List<TopicResp> loadTopics(String tag) {
        List<TopicResp> topicRespList = topicMapper.loadTopics(tag);
        for (int i = 0; i < topicRespList.size(); i++) {
            TopicResp dbTopicResp = topicRespList.get(i);

            //存在updaterId 添加更新者的登录名
            if (dbTopicResp.getUpdaterId() != null && !dbTopicResp.getUpdaterId().isEmpty()) {
                String updaterLoginName = commonMapper.getLoginNameById(dbTopicResp.getUpdaterId());
                //存在id却找不到这个用户 判定为已被删除用户
                dbTopicResp.setUpdaterLoginName(updaterLoginName == null ? "已注销用户" : updaterLoginName);
                topicRespList.set(i, dbTopicResp);
            }
        }
        return topicRespList;
    }

    @Override
    public List<Tag> loadTags() {
        return topicMapper.loadTags();
    }

    @Override
    public List<TopicResp> loadMyTopics(String date, String loginName) {
        List<TopicResp> topicRespList = topicMapper.loadMyTopics(date, loginName);
        for (int i = 0; i < topicRespList.size(); i++) {
            TopicResp dbTopicResp = topicRespList.get(i);
            //存在updaterId 添加更新者的登录名
            if (dbTopicResp.getUpdaterId() != null && !dbTopicResp.getUpdaterId().isEmpty()) {
                String updaterLoginName = commonMapper.getLoginNameById(dbTopicResp.getUpdaterId());
                //存在id却找不到这个用户 判定为已被删除用户
                dbTopicResp.setUpdaterLoginName(updaterLoginName == null ? "已注销用户" : updaterLoginName);
                topicRespList.set(i, dbTopicResp);
            }
        }
        return topicRespList;
    }

    @Override
    public int deleteTopic(String id) {
        return topicMapper.deleteTopicById(id);
    }

    @Override
    public int updateTopic(Topic topic) {
        if (topic != null) {
            return topicMapper.updateTopic(topic);
        }
        return 0;
    }

    @Override
    public List<TopicResp> loadFollowedTopics(String tag, String loginName) {
        List<TopicResp> topicRespList = topicMapper.loadFollowedTopics(tag, loginName);
        for (int i = 0; i < topicRespList.size(); i++) {
            TopicResp dbTopicResp = topicRespList.get(i);
            //存在updaterId 添加更新者的登录名
            if (dbTopicResp.getUpdaterId() != null && !dbTopicResp.getUpdaterId().isEmpty()) {
                String updaterLoginName = commonMapper.getLoginNameById(dbTopicResp.getUpdaterId());
                //存在id却找不到这个用户 判定为已被删除用户
                dbTopicResp.setUpdaterLoginName(updaterLoginName == null ? "已注销用户" : updaterLoginName);
                topicRespList.set(i, dbTopicResp);
            }
            //若话题已删除
            if (dbTopicResp.getDelFlag().equals("1")) {
                dbTopicResp.setTitle("已删除");
                dbTopicResp.setContent("已失效的话题");
            }
        }
        return topicRespList;
    }

    @Override
    public int unfollowTopic(String id) {
        return topicMapper.unfollowTopicByTopicId(id);
    }

    @Override
    public int followTopic(String topicId, String loginName) {

        User dbUser = userMapper.getByLoginName(loginName);
        if (dbUser == null) {
            return 0;   //若找不到用户信息直接返回
        }

        //判断用户是否已关注
        String userId = dbUser.getId();
        List<FollowedTopic> followedTopicList = topicMapper.loadFollowedTopicsByCreatorId(userId);
        for (FollowedTopic dbfollowedTopic : followedTopicList) {
            if (dbfollowedTopic.getTopicId().equals(topicId)) {
                return 0;
            }
        }

        //执行插入
        FollowedTopic follow = new FollowedTopic();
        follow.setId(UUID.randomUUID().toString().replace("-",""));
        follow.setCreateTime(new Date());
        follow.setTopicId(topicId);
        follow.setCreatorId(userId);

        return topicMapper.addFollowTopic(follow);
    }

    @Override
    public int addTopic(Topic topic, String loginName) {
        String userId = "";
        User user = userMapper.getByLoginName(loginName);
        if (user != null) {
            userId = user.getId();
        }
        topic.setCreatorId(userId);
        topic.setCreateTime(new Date());
        topic.setId(UUID.randomUUID().toString().replace("-", ""));

        return topicMapper.addTopic(topic);
    }

    @Override
    public int modifyTopic(Topic topic, String loginName) {
        String userId = "";
        User user = userMapper.getByLoginName(loginName);
        if (user != null) {
            userId = user.getId();
        }
        topic.setUpdaterId(userId);
        topic.setUpdateTime(new Date());

        return topicMapper.updateTopic(topic);
    }

    @Override
    public Topic getTopicById(String id) {
        return topicMapper.getTopicById(id);
    }

    @Override
    public TopicResp getTopicRespById(String id) {
        TopicResp dbTopicResp = topicMapper.getTopicRespById(id);
        //存在updaterId 添加更新者的登录名
        if (dbTopicResp.getUpdaterId() != null && !dbTopicResp.getUpdaterId().isEmpty()) {
            String updaterLoginName = commonMapper.getLoginNameById(dbTopicResp.getUpdaterId());
            //存在id却找不到这个用户 判定为已被删除用户
            dbTopicResp.setUpdaterLoginName(updaterLoginName == null ? "已注销用户" : updaterLoginName);
        }
        return dbTopicResp;
    }


    /**
     * 管理员页面获取话题列表方法
     * @param curr 当前页
     * @param limit 每页条数
     * @param query 查询条件
     * @return 完整话题信息
     */
    @Override
    public List<TopicResp> loadTopicList(int curr, int limit, String query) {
        int startIndex = curr * limit - limit;
        if (query == null) {
            query = "";
        }
        List<TopicResp> list = topicMapper.loadTopicList(startIndex, limit, query);
        //查出每个话题的留言数
        if (list!=null&&!list.isEmpty()){
            for (int i=0;i<list.size();i++){
                TopicResp topicResp = list.get(i);
                int commentCount = commentMapper.countByTopicId(topicResp.getId());
                topicResp.setCommentCount(commentCount);
                list.set(i,topicResp);
            }
        }
        return list;
    }
}
